<?php

namespace Home\Model;

class TeamPlayerModel extends CommonModel {

    public function createTeamPlayer($dt=array(), $is_in_charge=false){
        $tm = time();

        if (!$is_in_charge) {
            $dt['is_in_charge'] = 0;
        }
        $dt['is_in'] = 1;
        $dt['create_time'] = $tm;
        $dt['update_time'] = $tm;

        $rs = $this->add($dt);

        $ret = array();
        if ($rs) {
            $ret['status'] = true;
            $ret['msg'] = '操作成功！';
        } else {
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
        }
        return $ret;
    }

    public function join($dt){
        $rs = $this->where($dt)->find();
        $ret = array();
        if ($rs) {
            $ret['status'] = true;
            $ret['msg'] = '操作成功！';
            return $ret;
        }

        $tm = time();

        $dt['is_in_charge'] = 0;
        $dt['is_in'] = 1;
        $dt['create_time'] = $tm;
        $dt['update_time'] = $tm;

        $rs = $this->add($dt);

        if ($rs) {
            $ret['status'] = true;
            $ret['msg'] = '操作成功！';
        } else {
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
        }
        return $ret;
    }

    public function left($dt){
        $rs = $this->where($dt)->delete();

        $ret = array();
        if ($rs) {
            $ret['status'] = true;
            $ret['msg'] = '操作成功！';
        } else {
            $ret['status'] = false;
            $ret['msg'] = $this->getError();
        }
        return $ret;
    }

    public function findTeamPlayerByPlayerId($player_id){
        $flt = array(
            'player_id'=>$player_id
        );
        $dt = $this->where($flt)->find();
        return $dt;
    }

    public function findPlayerByTeamId($team_id){
        $flt = array(
            'team_id'=>$team_id
        );
        $dt = $this->where($flt)->select();
        return $dt;
    }


    public function chargeList($player_id){
        $flt = array(
            'player_id' => $player_id,
            'is_in_charge' => 1,
            'is_in' => 1,
        );
        return $this->where($flt)->select();
    }
}
